package io.qar.commons.mybatis.utils;

import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;


@Data
public class Page<T> {
    private Integer totalSize; // 总页数
    private Integer total; // 总记录数
    @NotNull(message = "每页大小pageSize不能为空")
    @Min(value = 1, message = "每页大小pageSize必须大于等于1")
    private Integer limit; // 每页数量
    @NotNull(message = "当前页码curPage不能为空")
    @Min(value = 1, message = "当前页码curPage必须大于等于1")
    private Integer page; // 当前页
    private List<T> list;

    public Page(List<T> data, Long total, Integer curPage, Integer pageSize) {
        this.list = data;
        this.total = Integer.parseInt(String.valueOf(total));
        this.page = curPage != null && curPage >= 1 ? curPage : 1;
        this.limit = pageSize != null && pageSize >= 1 ? pageSize : 10; // 假设默认每页10条记录

        this.totalSize = (int) Math.ceil((double) total / pageSize);
    }
}
